<template>
  <view>
    <!-- 拟搜索框组件 -->
    <!-- <MySearch /> -->
    <view class="search-box">
      <my-search
        @click="gotoSearch"
        bgColor="#0000ff"
        bdRadius="10rpx"
        tipWords="搜你想要..."
      />
    </view>
    <!-- 轮播图组件 -->
    <swiper
      indicator-dots
      autoplay
      circular
      :interval="3000"
      :duration="1000"
      class="top-swiper"
    >
      <swiper-item v-for="(item, index) in swiperList" :key="index">
        <navigator
          :url="`/pages_sub1/product-detail/product-detail?pid=${item.goods_id}`"
          class="swiper-content"
        >
          <image :src="item.image_src"></image>
        </navigator>
      </swiper-item>
    </swiper>

    <!-- 分类导航区域 -->
    <view class="nav-list">
      <view
        class="nav-item"
        v-for="(item, index) in navList"
        :key="index"
        @click="navClickHandler(item)"
      >
        <image :src="item.image_src" class="nav-img"></image>
      </view>
    </view>

    <!-- 楼层 -->
    <view class="floor-list">
      <!-- 楼层 item 项 -->
      <view class="floor-item" v-for="(item, index) in floorList" :key="index">
        <!-- 楼层标题 -->
        <image :src="item.floor_title.image_src" class="floor-title"></image>
        <!-- 楼层图片区域 -->
        <view class="floor-img-box">
          <!-- 左侧 1 个大图 -->
          <view
            class="left-img-box"
            @click="goProductList(item.product_list[0])"
          >
            <image
              :src="item.product_list[0].image_src"
              :style="{ width: `${item.product_list[0].image_width}rpx` }"
              mode="widthFix"
            ></image>
          </view>

          <!-- 右侧 4 个小图 -->
          <view class="right-img-box">
            <block v-for="(item1, index1) in item.product_list" :key="index1">
              <view
                class="right-img-item"
                v-if="index1 > 0"
                @click="goProductList(item1)"
              >
                <image
                  :src="item1.image_src"
                  :style="{ width: `${item1.image_width}rpx` }"
                  mode="widthFix"
                ></image>
              </view>
            </block>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
import MySearch from "@/components/my-search.vue";
import badge from "@/mixins/badge.js";
export default {
  mixins: [badge],
  components: {
    MySearch,
  },
  data() {
    return {
      // 1. 轮播图数据数组
      swiperList: [],
      // 2. 金刚区数据
      navList: [],
      // 3. 楼层数据
      floorList: [],
    };
  },
  created() {
    this.getSwiperList();
    this.getNavList();
    this.getFloorList();
  },
  methods: {
    // 1. 获取轮播图数据
    async getSwiperList() {
      const { meta, message } = await this.$http.get("/home/swiperdata");
      if (meta.status !== 200) {
        return this.$msg();
      }
      this.swiperList = message;
    },
    // 2. 获取金刚区数据
    async getNavList() {
      const { meta, message } = await this.$http.get("/home/catitems");
      if (meta.status !== 200) {
        return this.$msg();
      }
      this.navList = message;
    },
    // 3. 金刚区点击
    navClickHandler(item) {
      if (item.name === "分类") {
        uni.switchTab({
          url: "/pages/category/category",
        });
      }
    },
    // 4. 获取楼层数据
    async getFloorList() {
      const { meta, message } = await this.$http.get("/home/floordata");
      if (meta.status !== 200) {
        return this.$msg();
      }
      this.floorList = message;
    },
    // 5. 跳转去商品列表页
    goProductList(item) {
      // console.log(item);
      // 5.1 截取传递的字符串
      const str = item.navigator_url.split("?")[1];
      // console.log(str);
      // 5.2 跳转到商品列表页
      uni.navigateTo({
        url: `/pages_sub1/product-list/product-list?${str}`,
      });
    },
    // 6. 去搜索页面
    gotoSearch() {
      uni.navigateTo({
        url: "/pages_sub1/search/search",
      });
    },
  },
};
</script>

<style lang="scss">
.top-swiper {
  height: 330rpx;

  .swiper-content,
  image {
    width: 100%;
    height: 100%;
  }
}

.nav-list {
  display: flex;
  justify-content: space-around;
  margin: 30rpx 0;

  .nav-img {
    width: 128rpx;
    height: 140rpx;
  }
}

.floor-title {
  display: flex;
  width: 100%;
  height: 60rpx;
}

.right-img-box {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-around;
}

.floor-img-box {
  display: flex;
  padding-left: 10rpx;
}

.search-box {
  position: sticky;
  top: 0;
  left: 0;

  z-index: 999;
}
</style>